module repository.AnnualReviewRepo;

import d2sqlite3;

import model.AnnualReview;
import utils.ConstValue;

/** 
 * 年度点评仓储类
 */
class AnnualReviewRepo
{

    Database db;
    this ()
    {
        db = Database(Const.connStr);
    }

    ~this ()
    {
        db.close();
    }

    /** 
     * 获取全部年度点评列表数据
     * Returns: AnnualReview[]
     */
    AnnualReview[] getAllAnnualReviewList ()
    {
        ResultRange results = db.execute("SELECT id, year, comment FROM annual_review;");

        AnnualReview[] annualReviewList = [];
        if (results.empty)
        {
            return annualReviewList;
        }
        foreach (Row row; results)
        {
            auto id = row["id"].as!uint;
            auto year = row["year"].as!uint;
            auto comment = row["comment"].as!string;

            annualReviewList ~= new AnnualReview(id, year, comment);
        }

        return annualReviewList;
    }

    /** 
     * 获取年度点评列表数据
     * Params:
     *   videoCate = uint = 视频类型（1：电视剧，2：电影，3：年度点评）
     * Returns: AnnualReview[]
     */
    AnnualReview[] getAnnualReviewList (uint videoCate)
    {
        Statement statement = db.prepare(
            "SELECT id, year, comment FROM annual_review WHERE video_cate=:video_cate;");
        statement.bind(":video_cate", videoCate);
        ResultRange results = statement.execute();

        AnnualReview[] annualReviewList = [];
        if (results.empty)
        {
            return annualReviewList;
        }
        foreach (Row row; results)
        {
            auto id = row["id"].as!uint;
            auto year = row["year"].as!uint;
            auto comment = row["comment"].as!string;

            annualReviewList ~= new AnnualReview(id, year, comment);
        }

        return annualReviewList;
    }

    /** 
     * 添加年度点评
     * Params:
     *   annualReview = AnnualReview = 年度点评AnnualReview实例
     * Returns: bool
     */
    bool addAnnualReview (AnnualReview annualReview)
    {
        Statement statement = db.prepare(
            "INSERT INTO annual_review (year, comment, video_cate) VALUES (:year, :comment, :video_cate)");
        statement.bindAll(annualReview.year, annualReview.comment, annualReview.videoCate);
        statement.execute();
        return db.changes > 0;
    }

    /** 
     * 删除年度点评
     * Params:
     *   annualReviewId    uint    id值
     * Returns: bool
     */
    bool removeAnnualReview (uint annualReviewId)
    {
        Statement statement = db.prepare("DELETE FROM annual_review WHERE id=:id");
        statement.bind(":id", annualReviewId);
        statement.execute();
        return db.changes > 0;
    }

    /** 
    * 更新年度点评信息
    * Params:
    *   annualReviewId = uint = id值
    *   annualReview = AnnualReview = AnnualReview实例
    * Returns: bool
    */
    bool updateAnnualReview (uint annualReviewId, AnnualReview annualReview)
    {
        Statement statement = db.prepare(
            "UPDATE annual_review SET year=:year, comment=:comment WHERE id=:id");
        statement.bindAll(annualReview.year, annualReview.comment, annualReviewId);
        statement.execute();
        return db.changes > 0;
    }
}
